package main

import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
	//"time"
)

func main() {
	db, err := sql.Open("mysql", `lwj:Ryt5mOTt@tcp(192.168.25.61:3306)/hh_dev?charset=utf8`)
	checkErr(err)

	//插入数据
	stmt, err := db.Prepare("INSERT user_info SET user_name=?,depart_name=?,create_times=?")
	checkErr(err)

	res, err := stmt.Exec("astaxie", "研发部门", "2012-12-09")
	checkErr(err)

	id, err := res.LastInsertId()
	checkErr(err)

	fmt.Println(id)
	//更新数据
	stmt, err = db.Prepare("update user_info set user_name=? where uid=?")
	checkErr(err)

	res, err = stmt.Exec("astaxieupdate", id)
	checkErr(err)

	affect, err := res.RowsAffected()
	checkErr(err)

	fmt.Println(affect)

	//查询数据
	rows, err := db.Query("SELECT * FROM user_info")
	checkErr(err)

	for rows.Next() {
		var uid int
		var user_name string
		var department_name string
		var create_time string
		err = rows.Scan(&uid, &user_name, &department_name, &create_time)
		checkErr(err)
		fmt.Println(uid)
		fmt.Println(user_name)
		fmt.Println(department_name)
		fmt.Println(create_time)
	}

	//删除数据
	stmt, err = db.Prepare("delete from user_info where uid=?")
	checkErr(err)

	res, err = stmt.Exec(id)
	checkErr(err)

	affect, err = res.RowsAffected()
	checkErr(err)

	fmt.Println(affect)

	db.Close()

}

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}
